# coding: utf-8
# walk でファイル探索

import os, re

# ----------------------------------------------------------------
# 
# ----------------------------------------------------------------
def getFileList(path):
    file_list = []
    for (root, dirs, files) in os.walk(path):
        for file in files:
            # パスセパレータは\\より/の方が好きなので置換
            file_list.append( os.path.join(root,file).replace("\\", "/") )
        
    return file_list

# ----------------------------------------------------------------
# 
# ----------------------------------------------------------------
def getDirList(path):
    dir_list = []
    for (root, dirs, files) in os.walk(path):
        for dir in dirs:
            # パスセパレータは\\より/の方が好きなので置換
            dir_list.append( os.path.join(root,dir).replace("\\", "/") )
        
    return dir_list

# ----------------------------------------------------------------
# 
# ----------------------------------------------------------------
def checkString(str, patterns):
    for pattern in patterns:
        if re.search(pattern, str):
            return True
        
    return False

# ================================================================
# main時のtest
# ================================================================
def main():
    # パターン
    patterns = ["\.py$"]
    # パス
    path = "C:/Python32"
    
    # ファイルを全て表示
    file_list = getFileList(path)
    for f in file_list:
        if checkString(f, patterns): print(f)

    # ディレクトリを全て表示
    dir_list = getDirList(path)
    for d in dir_list:
        if checkString(d, patterns): print(d)
    

# メイン時
if __name__ == '__main__': main()







